package beer.coder.imageviewer.handler;

import beer.coder.imageviewer.ImageViewer;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

/**
 * TODO
 *
 * @author amath0312
 * @version 1.0
 * @date 2020-11-01 13:00
 */
public class ImageViewFitHandler implements EventHandler<MouseEvent> {

  protected ImageViewer imageView;

  public ImageViewFitHandler(ImageViewer imageView) {
    this.imageView = imageView;
  }

  @Override
  public void handle(MouseEvent event) {
    if (event.getEventType() == MouseEvent.MOUSE_CLICKED
        && event.getButton() == MouseButton.PRIMARY
        && event.getClickCount() == 3) {
      Bounds bounds = imageView.getParent().getLayoutBounds();
      imageView.setLayoutX(0);
      imageView.setLayoutY(0);
      imageView.setScaleX(1);
      imageView.setScaleY(1);
      imageView.setFitWidth(bounds.getWidth());
      imageView.setFitHeight(bounds.getHeight());
      imageView.resetZoom();
    }
  }
}
